//
//  ReplyCell.m
//  yunqiphone
//
//  Created by Maple on 14-3-17.
//  Copyright (c) 2014年 ci123.com. All rights reserved.
//

#import "ReplyCell.h"
#import "UIImageView+WebCache.h"
#import "ReplyModel.h"
#import "UIView+Additions.h"
#import "DetailNewViewController.h"

@implementation ReplyCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    [self _initView];
  }
  return self;
}

//初始化子视图
- (void)_initView {

    //头像
    _head = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 40, 40)];
    _head.backgroundColor = [UIColor clearColor];
    _head.userInteractionEnabled=YES;
    [[_head layer] setCornerRadius:20];
    [[_head layer] setMasksToBounds:YES];
    [self.contentView addSubview:_head];
   // UITapGestureRecognizer *ges=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToUserCenter:)];
    //[_head addGestureRecognizer:ges];

    //昵称
    _nickname=[[UILabel alloc] initWithFrame:CGRectMake(_head.right+10, _head.top + 13, 80, 15)];
    _nickname.backgroundColor = [UIColor clearColor];
    _nickname.font = [UIFont systemFontOfSize:14];
    _nickname.textColor = UIColorMake(144, 144, 144);
    _nickname.lineBreakMode = NSLineBreakByTruncatingTail;
    [self.contentView addSubview:_nickname];
    
    _medal = [[UILabel alloc] initWithFrame:CGRectMake(_nickname.left, _nickname.top, 46, 15)];
    _medal.textAlignment = NSTextAlignmentCenter;
    _medal.backgroundColor = DefaultMedalBackgroundColor;
    _medal.font = [UIFont systemFontOfSize:10.5f];
    _medal.textColor = [UIColor whiteColor];
    _medal.layer.cornerRadius = 2.0f;
    _medal.layer.masksToBounds = YES;
    _medal.hidden = YES;
    [self.contentView addSubview:_medal];

    _host = [[UILabel alloc]initWithFrame:CGRectMake(_nickname.left+10, _nickname.top + .5f, 28, 14)];
    [_host setText:@"楼主"];
    [_host setFont:[UIFont systemFontOfSize:11]];
    [_host setTextColor:[UIColor whiteColor]];
    [_host setTextAlignment:NSTextAlignmentCenter];
    [_host setBackgroundColor:RGBCOLOR(219,163,224)];
    [_host.layer setCornerRadius:3.0f];
    [_host.layer setMasksToBounds:YES];
    _host.hidden = YES;
    [self.contentView addSubview:_host];
    
    _showDated = [[UILabel alloc] initWithFrame:CGRectMake(_nickname.left-10, _nickname.top + .5f, 28, 14)];
    _showDated.backgroundColor = [UIColor clearColor];
    _showDated.font = [UIFont systemFontOfSize:11];
    _showDated.textColor = RGBCOLOR(151, 151, 151);
    [self.contentView addSubview:_showDated];

    //个人信息
    
    _floorLabel=[[UILabel alloc] initWithFrame:CGRectZero];
    _floorLabel.backgroundColor=[UIColor clearColor];
    _floorLabel.font=[UIFont systemFontOfSize:11];
    _floorLabel.textColor=RGBCOLOR(151, 151, 151);
    _floorLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_floorLabel];

    //是否有图片
    _pic=[[UIImageView alloc] initWithFrame:CGRectZero];
    _pic.backgroundColor = [UIColor clearColor];
    [_pic setUserInteractionEnabled:YES];
    _pic.contentMode = UIViewContentModeScaleAspectFill;
    [_pic.layer setMasksToBounds:YES];
    [self.contentView addSubview:_pic];
    UITapGestureRecognizer *showImageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImage:)];
    [_pic addGestureRecognizer:showImageTap];

    //内容
    _content = [[UILabel alloc] initWithFrame:CGRectZero];
    _content.backgroundColor = [UIColor clearColor];
    [_content setLineBreakMode:NSLineBreakByWordWrapping];
    _content.font = [UIFont fontWithName:@"Arial" size:16];
    _content.textColor = RGBCOLOR(112, 112, 112);
    [_content setNumberOfLines:0];  //必须是这组值
    [self.contentView addSubview:_content];

    //引用view
    _quoteView=[[UIImageView alloc] initWithFrame:CGRectZero];
    _quoteView.backgroundColor = [UIColor clearColor];
    [_quoteView setUserInteractionEnabled:YES];
    [self.contentView addSubview:_quoteView];
    UIImage *backImage = [UIImage imageNamed:@"detail_quote_bg"];
    backImage = [backImage resizableImageWithCapInsets:UIEdgeInsetsMake(18, 40, 8, 8)];
    [_quoteView setImage:backImage];

    //引用昵称
    _quoteNick=[[UILabel alloc] initWithFrame:CGRectMake(10, 25, 140, 14)];
    _quoteNick.font=[UIFont systemFontOfSize:13];
    _quoteNick.backgroundColor=[UIColor clearColor];
    [_quoteView addSubview:_quoteNick];
    
    _quoteFloorLabel = [[UILabel alloc] initWithFrame:CGRectMake(ContentWidth - 90, _quoteNick.top, 60, 14)];
    [_quoteFloorLabel setBackgroundColor:[UIColor clearColor]];
    [_quoteFloorLabel setTextAlignment:NSTextAlignmentRight];
    [_quoteFloorLabel setTextColor:RGBCOLOR(151, 151, 151)];
    [_quoteFloorLabel setFont:[UIFont systemFontOfSize:12.0f]];
    [_quoteFloorLabel setHidden:YES];
    [_quoteView addSubview:_quoteFloorLabel];

    _quoteContent=[[UILabel alloc] initWithFrame:CGRectZero];
    [_quoteContent setNumberOfLines:0];  //必须是这组值
    _quoteContent.backgroundColor = [UIColor clearColor];
    _quoteContent.textColor = RGBCOLOR(109, 97, 83);
    _quoteContent.font = [UIFont fontWithName:@"Arial" size:14];
    [_quoteView addSubview:_quoteContent];

    //是否有图片
    _quotePic=[[UIImageView alloc] initWithFrame:CGRectZero];
    _quotePic.backgroundColor = [UIColor clearColor];
    _quotePic.contentMode = UIViewContentModeScaleAspectFill;
    [_quotePic.layer setMasksToBounds:YES];
    [_quoteView addSubview:_quotePic];
    UITapGestureRecognizer *showImageTap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showQuoteImage:)];
    [_quotePic addGestureRecognizer:showImageTap2];
    [_quotePic setUserInteractionEnabled:YES];

    
    
    _replyButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [_replyButton setTitle:@"回复" forState:UIControlStateNormal];
    [_replyButton setImage:[UIImage imageNamed:@"comment_blue"] forState:UIControlStateNormal];
    [_replyButton setTitleColor:UIColorMake(120, 165, 212) forState:UIControlStateNormal];
    [_replyButton setShowsTouchWhenHighlighted:YES];
    [self.contentView addSubview:_replyButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    DetailNewViewController *viewControll = (DetailNewViewController *)[self viewController];

    //头像
//    [_head sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",_avatar_header,_data.replyUserHead]] placeholderImage:[UIImage imageNamed:@"default_circle_icon"]];
     [_head sd_setImageWithURL:[NSURL URLWithString:_data.replyUserHead] placeholderImage:[UIImage imageNamed:@"default_avatar"]];
    _head.tag = _data.replyUserId != nil ? [_data.replyUserId integerValue] : 0;
    
    _nickname.text = [NSString stringWithFormat:@"%@",_data.replyUserNick];
    _nickname.tag = _data.replyUserId != nil ? [_data.replyUserId integerValue] : 0;
    CGSize nicknameSize = [_nickname.text sizeWithFont:_nickname.font
                                     constrainedToSize:CGSizeMake(1000, 15)
                                         lineBreakMode:NSLineBreakByTruncatingTail];
    float marginLeft;
    float nicknameWidth = 90;
    if (nicknameSize.width < nicknameWidth) {
        _nickname.width = nicknameSize.width;
    }else {
        _nickname.width = nicknameWidth;
    }
    marginLeft = _nickname.right + 8;
    
    float contentX = _head.right + 10 ;

    //楼层
    _floorLabel.frame = CGRectMake(APP_SCREEN_WIDTH - 35, _nickname.top+2, 30, 12);
    if ([_floor integerValue] > 0) {
        [_floorLabel setHidden:NO];
        _floorLabel.text = [NSString stringWithFormat:@"%@楼",_floor];
    }else {
        [_floorLabel setHidden:YES];
    }
    
    _showDated.frame = CGRectMake(_floorLabel.left -113,_nickname.top+2,110,12);

    //标题
    NSString *nicks=[NSString stringWithFormat:@"%@",_data.replyContent];
    CGSize size = CGSizeMake(ContentWidth, 2000);
    CGSize labelsize = [nicks sizeWithFont:_content.font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
    _content.frame = CGRectMake(contentX, _head.bottom+10, ContentWidth, labelsize.height + 4);
    _content.text = nicks;
    //_content.text = [nicks replaceFaceWithDictionary:faceDic];

    int top = _content.bottom + 10;
    _data.replyUserPic = [NSString stringWithFormat:@"%@",_data.replyUserPic];
    if(_data.replyUserPic.length>6 &&! [_data.replyUserPic isEqualToString:@"<null>"]){
        
        int reW = [[NSString stringWithFormat:@"%@", _data.replyUserPicWidth] intValue];
        int reH = [[NSString stringWithFormat:@"%@", _data.replyUserPicHeight] intValue];     float pic_w = ContentWidth - 10;
        float pic_h = pic_w;
        if(reW != 0 && reH != 0){
            pic_h = (int)(pic_w * reH / reW);
        }
        _pic.frame = CGRectMake(contentX + (ContentWidth - pic_w) / 2, _content.bottom + 10, pic_w, pic_h);
        [_pic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",_avatar_header,_data.replyUserPic]] placeholderImage:[UIImage imageNamed:@"detail_picloading_bg"]];
        [_pic setHidden:NO];
        top = _pic.bottom+25;
    }else{
        [_pic setHidden:YES];
        _pic.image = nil;
    }

    int conHeight=top;
    
    if(_data.replyQuoteNick.length>0) {
        _quoteNick.text = [NSString stringWithFormat:@"%@:",_data.replyQuoteNick];
        _quoteNick.hidden = NO;
        /*
        if ([_data.replyQuoteFloor integerValue] > 0) {
            [_quoteFloorLabel setText:[NSString stringWithFormat:@"%@楼", _data.replyQuoteFloor]];
            [_quoteFloorLabel setHidden:YES];
        }else {
            [_quoteFloorLabel setHidden:YES];
       }*/
    }else{
        _quoteNick.hidden = YES;
    }

    int quote_pic_height = 0;
    int picWidth = ContentWidth - 20;


    if(_data.replyQuoteUid != nil && [_data.replyQuoteUid integerValue]>0){
        NSString *nicks=[NSString stringWithFormat:@"%@",_data.replyQuoteContent];
        CGSize size = CGSizeMake(picWidth,2000);
        CGSize labelsize = [nicks sizeWithFont:_quoteContent.font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
        _quoteContent.frame = CGRectMake(10, _quoteNick.bottom, picWidth, labelsize.height+15 );
        _quoteContent.text = nicks;
        _quoteView.frame = CGRectMake(contentX, top, ContentWidth, 60+labelsize.height+quote_pic_height);
        _quoteView.hidden = NO;
        _quoteContent.hidden = NO;
        top = _quoteView.bottom + 20;
    }else{
        _quoteView.height = 0;
        _quoteView.hidden = YES;

        _quoteContent.hidden = YES;
        top = conHeight;
    }


    //时间
   
    _showDated.text = [NSString stringWithFormat:@"%@",_data.replyShowDated];

    //回复按钮
    _replyButton.frame = CGRectMake(APP_SCREEN_WIDTH - 73,  top - 4.5, 80, 21);
    _replyButton.tag = [_data.replyId integerValue];
    _replyButton.titleLabel.font = [UIFont systemFontOfSize:12];
    UITapGestureRecognizer *replyGes=[[UITapGestureRecognizer alloc] initWithTarget:viewControll action:@selector(getReplyBox:)];
    [_replyButton addGestureRecognizer:replyGes];
}

- (void)showImage:(UITapGestureRecognizer *)tapReconizer
{
    UIImageView *picView = (UIImageView *)tapReconizer.view;
    
    originFrame = [picView convertRect:picView.bounds toView:nil];
    UIViewController *viewController = [[UIViewController alloc]init];
    viewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    viewController.view.frame = CGRectMake(0, 0, APP_SCREEN_WIDTH, APP_SCREEN_HEIGHT);
    viewController.view.backgroundColor = RGBACOLOR(33, 33, 33, 0.5);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage)];
    viewController.view.userInteractionEnabled = YES;
    [viewController.view addGestureRecognizer:tap];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:originFrame];
    imageView.tag = 1;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    //imageView.image = picView.image;

    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _avatar_header,_data.replyUserPic]]
              placeholderImage:picView.image];
    [viewController.view addSubview:imageView];
    _showImageView = imageView;
    [[self viewController] presentViewController:viewController
                                        animated:NO
                                      completion:^(void){
                                          
                                          CGSize size = picView.image.size;
                                          CGSize fullSize = CGSizeMake(APP_SCREEN_WIDTH - 20, APP_SCREEN_HEIGHT - 40);
                                          float newWidth = size.width;
                                          float newHeight = size.height;
                                          float scale = size.width / size.height;
                                          if (scale >= (fullSize.width / fullSize.height)) {
                                              if (size.width > fullSize.width) {
                                                  newWidth = fullSize.width;
                                                  newHeight = newWidth / scale;
                                              }
                                          }else {
                                              if (size.height > fullSize.height) {
                                                  newHeight = fullSize.height;
                                                  newWidth = newHeight * scale;
                                              }
                                          }
                                          [UIView animateWithDuration:.3f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
                                              imageView.frame = CGRectMake((APP_SCREEN_WIDTH - newWidth) / 2, (APP_SCREEN_HEIGHT - newHeight) / 2, newWidth, newHeight);} completion:nil];
                                      }];
    
}

- (void)showQuoteImage:(UITapGestureRecognizer *)tapReconizer
{
    UIImageView *picView = (UIImageView *)tapReconizer.view;
    originFrame = [picView convertRect:picView.bounds toView:nil];
    UIViewController *viewController = [[UIViewController alloc]init];
    viewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    viewController.view.frame = CGRectMake(0, 0, APP_SCREEN_WIDTH, APP_SCREEN_HEIGHT);
    viewController.view.backgroundColor = RGBACOLOR(33, 33, 33, 0.5);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage)];
    viewController.view.userInteractionEnabled = YES;
    [viewController.view addGestureRecognizer:tap];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:originFrame];
    imageView.tag = 1;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    //[imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", _data.replyUserPic]]
    //          placeholderImage:picView.image];
    [viewController.view addSubview:imageView];
    _showImageView = imageView;
    [[self viewController] presentViewController:viewController
                                        animated:NO
                                      completion:^(void){
                                          
                                          CGSize size = picView.image.size;
                                          CGSize fullSize = CGSizeMake(APP_SCREEN_WIDTH - 20, APP_SCREEN_HEIGHT - 40);
                                          float newWidth = size.width;
                                          float newHeight = size.height;
                                          float scale = size.width / size.height;
                                          if (scale >= (fullSize.width / fullSize.height)) {
                                              if (size.width > fullSize.width) {
                                                  newWidth = fullSize.width;
                                                  newHeight = newWidth / scale;
                                              }
                                          }else {
                                              if (size.height > fullSize.height) {
                                                  newHeight = fullSize.height;
                                                  newWidth = newHeight * scale;
                                              }
                                          }
                                          
                                          [UIView animateWithDuration:.3f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
                                              imageView.frame = CGRectMake((APP_SCREEN_WIDTH - newWidth) / 2, (APP_SCREEN_HEIGHT - newHeight) / 2, newWidth, newHeight);} completion:nil];
                                      }];
}

- (void)hideImage
{
    [UIView animateWithDuration:0.3f animations:^(void){
        _showImageView.frame = originFrame;
        [[self viewController] performSelector:@selector(dismissModalViewControllerAnimated:) withObject:NO afterDelay:0.5];
    }];
}



@end
